//import fs from 'node:fs'
import { ShellExecution, Task, tasks, ProgressLocation, window, workspace } from 'vscode'
import * as cp from 'child_process';
import * as vscode from 'vscode';
import { error } from 'console';
import { SerialPort } from 'serialport';
const path = require('path');
const serialport_1 = require("serialport");

export const runUpload = () => {
	//window.showInformationMessage('Hello World000!');
	/*
	exec("dir",{ cwd: "" }).then((stdout, stderr)=>{
		//window.showInformationMessage('Hello World02!');
		
	})*/
	//window.showInformationMessage(workspace.getWorkspaceFolder)
	
	const port = new SerialPort({ path: 'COM3', baudRate: 9600 });
	if (port.isOpen){
		port.open()
	}
	port.on('error', err => {
		console.log('发生错误: ' + err.message + "\n");
	});

	port.on('data', data => {
		console.log('收到数据: ' + data + "\n");
	});
	port.open(function (err) {
		if (err) {
			console.log('端口打开失败: ' + err.message + "\n");return;
		}
		console.log('打开端口成功' + "\n");
	});  
	//serialport_1.SerialPort.list().then((ports) => {
	//	console.log(ports.path)
	//});
	/**
	const sshProcess = cp.spawn('ssh', ['-t','101.35.95.155', '-l root']);
	sshProcess.stdout.on('data', (data) => console.log(data.toString()));
	sshProcess.stderr.on('data', (data) => console.error(data.toString()));
 	*/
	const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!(workspaceFolders && workspaceFolders.length > 0)) {
		return
    } 
	const rootPath = workspaceFolders[0].uri.fsPath;
	/*
	traverseDirectory(rootPath+"/build", filePath => {
		console.log(filePath); // 打印文件路径
	});
	*/
	window.withProgress({
		location: ProgressLocation.Notification,
		title: "I am long running!",
		cancellable: true
	}, (progress, token) => {
		token.onCancellationRequested(() => {
			console.log("User canceled the long running operation");
		});
		progress.report({ increment: 0 });
		setTimeout(() => {
			progress.report({ increment: 10, message: "I am long running! - still going..." });
		}, 1000);
		setTimeout(() => {
			progress.report({ increment: 40, message: "I am long running! - still going even more..." });
		}, 2000);
		setTimeout(() => {
			progress.report({ increment: 50, message: "I am long running! - almost there..." });
			port.close();
		}, 3000);

		const p = new Promise<void>(resolve => {
			setTimeout(() => {
				resolve();
			}, 5000);
		});

		return p;
	});
	/*
	window.showInformationMessage('Hello World01!');
	if (stderr && stderr.length > 0) {
		window.showInformationMessage(stderr);
	}
	if (stdout) {
		window.showInformationMessage(stdout);
	}*/
}
/*
function exec(command: string, options: cp.ExecOptions): Promise<{ stdout: string; stderr: string }> {
	return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
		cp.exec(command, options, (error, stdout, stderr) => {
			if (error) {
				console.log("stderr");
				console.log(stderr);
				console.log("=============================");
				reject({ error, stdout, stderr });
			}else{
			    console.log(stdout);
				//resolve({ stdout, stderr });
			}
		});
	});
}
*/
/*
function traverseDirectory(dir, callback) {
  fs.readdirSync(dir).forEach(file => {
    const fullPath = path.join(dir, file);
    if (fs.lstatSync(fullPath).isDirectory()) {
      // 如果是文件夹，则递归遍历
      traverseDirectory(fullPath, callback);
    } else {
      // 如果是文件，则执行回调函数
      callback(fullPath);
    }
  });
}
  */